package NC11_将升序数组转化为平衡二叉搜索树;

import java.util.*;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
        this.right = null;
        this.right = null;
    }
}
public class Solution {
    /**
     *
     * @param num int整型一维数组
     * @return TreeNode类
     */
    public TreeNode sortedArrayToBST (int[] num) {
        // write code here
        if (num == null || num.length == 0){
            return null;
        }
        
        return sortedArrayToBST(num, 0, num.length - 1);
    }

    private TreeNode sortedArrayToBST(int[] num, int left, int right) {
        if (left > right){
            return null;
        }

        if (left == right){
            TreeNode node = new TreeNode(num[left]);
        }

        int len = right - left + 1;
        int mid = left + len / 2;

        TreeNode node = new TreeNode(num[mid]);


        node.left = sortedArrayToBST(num, left, mid - 1);
        node.right = sortedArrayToBST(num, mid + 1, right);
        return node;
    }
}